<template>
  <div class="app-container calendar-list-container">

    <div v-show="pageState === 1">
      <!-- 搜索栏 -->
      <div class="search__option">
        <div class="search__condition">
          区域名称
          <el-input class="search__input" v-model="params.label" placeholder="请输入区域名称"></el-input>
        </div>
        <div class="search__condition">
          区域类型
          <el-select v-model="params.type" clearable placeholder="请选择">
            <el-option v-for="item in [{ value: '', label: '全部' }, ...areaTypeOptions]" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </div>
        <div class="search__condition">
          父级区域名称
          <el-input class="search__input" v-model="params.parentLabel" placeholder="请输入父级区域名称"></el-input>
        </div>
        <div class="search__condition">
          <el-button @click="hanldeSearchData" type="primary">查询</el-button>
          <el-button @click="pageState = 2" type="success">新增</el-button>
        </div>
      </div>

      <!-- 列表 -->
      <el-table border :data="tableData" v-loading="loading" style="width: 100%">
        <el-table-column align="center" prop="label" label="区域名称">
        </el-table-column>
        <el-table-column align="center" prop="typeDesc" label="区域类型">
        </el-table-column>
        <el-table-column align="center" label="父级区域名称">
          <template slot-scope="{row}">
            {{row | tranFatherLabel}}
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态">
          <template slot-scope="{row}">
            {{ row.deleted | statusFilter }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" width=" 180">
          <template slot-scope="{row}">
            <el-button @click="hanldeEditArea(row.id)" size="small">编辑</el-button>
            <el-button type="primary" size="small" 
            @click="handeOpreate(row,1)" v-if="row.deleted == 0">无效</el-button>
            <el-button type="primary" size="small" 
            @click="handeOpreate(row,0)" v-else>有效</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div v-show="!loading" class="pagination">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="params.pageNo" :page-sizes="[5, 10, 20, 30, 50]" :page-size="params.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" />
      </div>

    </div>

    <!-- 区域新增 -->
    <area-add v-show="pageState === 2" @cancel="pageState = 1" @add="handleAddAreaSuccess" :details="details"/>

    <!-- 区域编辑 -->
    <area-edit :editId="editId" :dialog="editDialog" @edit="handleSaveAreaSuccess" @cancel="editDialog = false" />

  </div>
</template>

<script>
import { mapActions, mapGetters } from 'vuex'
import { getAreaList, operate } from '@/api/adminArea'
import AreaEdit from './adminArea/areaEdit'
import AreaAdd from './adminArea/areaAdd'

export default {
  name: 'AdminArea',
  data() {
    return {
      details:0,
      loading: false,
      pageState: 1,
      editId: 0,
      editDialog: false,
      total: 0,
      tableData: [],
      params: {
        pageSize: 10,
        pageNo: 1,
        label: '',
        parentLabel: '',
        type: ''
      }
    }
  },

  created() {
    this.searchData()
    this.GetAreaTypeOptions()
    this.GetAreaCascadeOptions()
  },

  computed: {
    ...mapGetters(['areaTypeOptions', 'areaCascadeOptions'])
  },

  methods: {
    // 映射 Actions
    ...mapActions(['GetAreaTypeOptions', 'GetAreaCascadeOptions', 'GetAreaCascadeSupplyOptions']),
    handeOpreate(row, type){
      operate({ 'areaId':row.id, 'action':type }).then(res => {
        const { code } = res
        if(code == 0){
          this.$notify({
            message: `${type == 1 ? '无效' : '有效'}成功`,
            type: 'success',
            duration: 2000
          })
          row['deleted'] = type
          this.details = { 'areaId':row.id, 'action':type }
        }
      })
    },
    // 列表数据
    searchData() {
      this.loading = true
      this.tableData = []
      getAreaList(this.params)
        .then(({ data = {} }) => {
          this.loading = false
          this.tableData = data.list
          this.total = data.total
        })
        .catch(() => {
          this.loading = false
        })
    },

    // 显示编辑
    hanldeEditArea(id) {
      this.editId = id
      this.editDialog = true
    },

    // 新增完成
    handleAddAreaSuccess() {
      this.pageState = 1
      this.searchData()
      this.details = { 'areaId':0, 'action':1 }
    },

    // 编辑完成
    handleSaveAreaSuccess() {
      this.editDialog = false
      this.searchData()
    },

    // 查询
    hanldeSearchData() {
      this.params.pageNo = 1
      this.searchData()
    },

    handleSizeChange(size) {
      this.params.pageSize = size
      this.searchData()
    },

    handleCurrentChange(page) {
      this.params.pageNo = page
      this.searchData()
    }
  },

  filters: {
    statusFilter(status) {
      const statusMap = {
        '1':'无效',
        '0':'有效'
      }
      return statusMap[status]
    },
    tranFatherLabel({ fatherLabel, type }) {
      const types = [1, 8, 9]
      if (!fatherLabel && types.includes(type)) return '中国'
      return fatherLabel
    }
  },

  components: {
    AreaEdit,
    AreaAdd
  }
}
</script>

<style lang="scss" scoped>
.search__option {
  margin-bottom: 20px;
  .search__condition {
    display: inline-block;
    padding-right: 10px;
    padding-bottom: 20px;
  }
  .search__input {
    padding-left: 10px;
    width: 200px;
  }
}
.pagination {
  margin-top: 20px;
}
</style>




